package com.csh.szhm.fragment;

import com.android.volley.VolleyError;
import com.csh.szhm.custorm.CustomProgressDialog;
import com.csh.szhm.custorm.CustomProgressDialog.onCencalProGress;
import com.csh.szhm.http.HttpCallBackUi;
import com.csh.szhm.http.HttpRequestTool;
import com.csh.szhm.utils.ActivityStartAndFinshAnimation;
import com.google.gson.reflect.TypeToken;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public abstract class MyBaseFragment<T> extends Fragment implements
		HttpCallBackUi<T> {
	protected View mView;
	protected int mViewId;
	protected HttpRequestTool<T> mHttpRequestTool;
	protected TypeToken<T> mTypeToken;
	protected String mRequestType;

	public MyBaseFragment(int view) {
		mViewId = view;
	}

	public MyBaseFragment() {
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		init();
		super.onActivityCreated(savedInstanceState);
	}

	private boolean isFirstSuccess;

	protected abstract void init();

	public void notifyData() {
		if (!isFirstSuccess) {
			requestNet();
		}
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		mView = inflater.inflate(mViewId, container, false);
		return mView;
	}

	public void startMyActivity(Class cls) {
		Intent intent = new Intent();
		intent.setClass(getActivity(), cls);
		startActivity(intent);
		ActivityStartAndFinshAnimation.ActivityStartAnimation(getActivity());
	}

	public void startMyActivity(Intent in, Class cls) {
		in.setClass(getActivity(), cls);
		startActivity(in);
		ActivityStartAndFinshAnimation.ActivityStartAnimation(getActivity());
	}

	private CustomProgressDialog mCustomProgressDialog;

	public void shouCustomProgressDialog() {
		if (mCustomProgressDialog == null) {
			mCustomProgressDialog = CustomProgressDialog
					.createDialog(getActivity());
			mCustomProgressDialog.setOnCancelProGress(new onCencalProGress() {
				@Override
				public void dissmis() {
				}
			});
		}
		mCustomProgressDialog.show();
	}

	public void dissmisCustomProgressDialog() {
		if (mCustomProgressDialog != null && mCustomProgressDialog.isShowing()) {
			mCustomProgressDialog.dismiss();
		}
	}

	protected void requestNet() {
		shouCustomProgressDialog();
	};

	@Override
	public void success(T data) {
		dissmisCustomProgressDialog();
		isFirstSuccess = true;

	}

	@Override
	public void failed(VolleyError error) {
		dissmisCustomProgressDialog();

	}

	@Override
	public void requestType(String Type) {
		mRequestType = Type;

	}

}
